package com.sanjiahantang.youjiarole.module.common.views.impl;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.FragmentManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.sanjiahantang.uilibrary.R;
import com.sanjiahantang.uilibrary.drawable.CartDrawable;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.jpush.JPushBoardCastReceiver;
import com.sanjiahantang.youjiarole.jpush.JPushUtil;
import com.sanjiahantang.youjiarole.module.apk.ApkDownloadUtils;
import com.sanjiahantang.youjiarole.module.common.presenters.MainPresenter;
import com.sanjiahantang.youjiarole.module.common.views.MainView;
import com.sanjiahantang.youjiarole.module.me.order.views.impl.OrderTakeFragment;
import com.sanjiahantang.youjiarole.module.project.views.impl.ProjectListFragment;

public class MainActivity extends BaseMvpActivity<MainView, MainPresenter> implements MainView,
        RadioGroup.OnCheckedChangeListener {

    private RadioButton rbHome;
    private RadioButton rbCategory;
    private RadioButton rbDecorate;
    private RadioButton rbShoppingCart;
    private RadioButton rbMe;
    private RadioGroup radioGroup;
    //用来确保apk更新只被执行一次
    private boolean isUpdate = true;


    public static boolean isForeground = false;

    @Override
    protected void onResume() {
        presenter.onResume($this);
        super.onResume();
        if (isUpdate) {
            isUpdate = false;
            ApkDownloadUtils.versionCheck(this, 0);
        }
    }

    // 点击返回按钮
    @Override
    public void onBackPressed() {
        presenter.onBackPressed();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        Class fragmentClass = null;
        String tag = null;
        int index = 0;
        boolean flag = false;
        switch (checkedId) {
            case R.id.rbHome:
                tag = TAG_HOME;
                fragmentClass = HomeFragment.class;
                break;
            case R.id.rbProject:
                index = 1;
                fragmentClass = ProjectListFragment.class;
                tag = TAG_PROJECT;
                break;
            case R.id.rbOrderTake:
                index = 2;
                fragmentClass = OrderTakeFragment.class;
                tag = TAG_ORDER_TAKE;
                break;
            case R.id.rbShoppingCart:
                index = 3;
                fragmentClass = ShoppingCartFragment.class;
                tag = TAG_CART;
                flag = true;
                break;
            case R.id.rbMe:
                index = 4;
                fragmentClass = MeFragment.class;
                tag = TAG_ME;
                break;
            default:
                break;

        }
        presenter.fragmentSelected($this, fragmentClass, R.id.fragmentRoot, tag, index, flag);


    }

    @Override
    public MainPresenter initPresenter() {
        return new MainPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        rbHome = (RadioButton) findViewById(R.id.rbHome);
        rbCategory = (RadioButton) findViewById(R.id.rbProject);
        rbShoppingCart = (RadioButton) findViewById(R.id.rbShoppingCart);
        rbDecorate = (RadioButton) findViewById(R.id.rbOrderTake);
        rbMe = (RadioButton) findViewById(R.id.rbMe);


    }

    @Override
    public void initData() {
        presenter.onLoad($this);
        presenter.initFragment(R.id.fragmentRoot, TAG_HOME);
        presenter.registerBroadcastReceiver($this);
        dealBottomButtonsClickEvent();
        presenter.getPersonalInfo($this);
        int page = getIntent().getIntExtra(KEY_PAGE, 0);
        if (page > 0) {
            setFragmentIndex(page);
        }
        if (getIntent().getStringExtra("First") != null && getIntent().getStringExtra("First").equals("True")) {
            setFragmentIndex(4);
        }
    }

    /**
     * 处理底部点击事件
     */
    @Override
    public void dealBottomButtonsClickEvent() {
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void close() {
        $this.finish();
    }

    public void setFragmentIndex(int index) {
        switch (index) {
            case 0:
                rbHome.setChecked(true);
                radioGroup.check(R.id.rbHome);
                break;

            case 1:
                rbCategory.setChecked(true);
                radioGroup.check(R.id.rbProject);
                break;
            case 2:
                rbDecorate.setChecked(true);
                radioGroup.check(R.id.rbOrderTake);
                break;
            case 3:
                rbShoppingCart.setChecked(true);
                radioGroup.check(R.id.rbShoppingCart);
                break;
            case 4:
                rbMe.setChecked(true);
                radioGroup.check(R.id.rbMe);
                break;
        }
    }


    @Override
    public void startFragment(int i) {
        Class fragment = null;
        int rootId = R.id.fragmentRoot;
        String tag = null;
        switch (i) {
            case 0:
                fragment = HomeFragment.class;
                tag = TAG_HOME;
                break;
            case 1:
                fragment = ProjectListFragment.class;
                tag = TAG_PROJECT;
                break;
            case 2:
                fragment = OrderTakeFragment.class;
                tag = TAG_ORDER_TAKE;
                break;
            case 3:
                fragment = ShoppingCartFragment.class;
                tag = TAG_CART;
                break;
            case 4:
                fragment = MeFragment.class;
                tag = TAG_ME;
                break;
        }
        presenter.startFragment(fragment, rootId, tag);
    }

    @Override
    public CartDrawable getCartDrawable(int i) {
        return (CartDrawable) rbShoppingCart.getCompoundDrawables()[i];
    }

    @Override
    public boolean isFirstPage() {
        return presenter.isFirstPage($this, rbHome, TAG_HOME);
    }

    @Override
    public FragmentManager getFManager() {
        return getSupportFragmentManager();
    }


    @Override
    public void setButtonImage(CartDrawable cartDrawable) {
        rbShoppingCart.setCompoundDrawablesWithIntrinsicBounds(null, cartDrawable, null, null);
    }

    @Override
    public void showProgress() {
        showProgress($this);
    }

    @Override
    public void hideProgress() {
        super.hideProgress();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getSupportFragmentManager().findFragmentById(R.id.fragmentRoot).onActivityResult(requestCode, resultCode, data);
    }

    private MessageReceiver mMessageReceiver;
    public static final String MESSAGE_RECEIVED_ACTION = "com.sanjiahantang.youjiarole.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";

    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        JPushBoardCastReceiver.getInstance(this).registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
                    String messge = intent.getStringExtra(KEY_MESSAGE);
                    String extras = intent.getStringExtra(KEY_EXTRAS);
                    StringBuilder showMsg = new StringBuilder();
                    showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
                    if (!JPushUtil.isEmpty(extras)) {
                        showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
                    }
                    setCostomMsg(showMsg.toString());
                }
            } catch (Exception e) {
            }
        }
    }

    private void setCostomMsg(String msg) {
        Utils.showToast($this, msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.unRegisterBroadcastReceiver(this);
    }
}
